package com.dongbao.util;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.file.impl.FileManager;
import com.intellij.util.WaitFor;
import com.intellij.util.io.ReadOnlyAttributeUtil;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class FileUtil {

    private static final Disposable myDisposable = Disposer.newDisposable();

    /**
     * 设置文件只读状态
     * */
    public static void makeFileReadOnly(Project myProject, VirtualFile file) {

        try {
            ReadOnlyAttributeUtil.setReadOnlyAttribute(file, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Disposable getTestRootDisposable() {
        return myDisposable;
    }

    /**
     * 从压缩包内复制文件到指定目录
     * @param file  jar包完整目录
     * @param fileName  fileNamejar包中文件名
     * @param tagPath   tagPath 输出目录
     */
    public static void copyMainfest(File file, String fileName, File mainfestFile) {
        ZipFile zipFile = null;
        ZipInputStream zipInput = null;
        ZipEntry zipEntry = null;
        OutputStream os = null;
        InputStream is = null;

        if(!mainfestFile.exists()){
            try {
                if(!mainfestFile.getParentFile().exists()){
                    mainfestFile.getParentFile().mkdirs();
                }
                mainfestFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            zipFile = new ZipFile(file);
            zipInput = new ZipInputStream(new FileInputStream(file), Charset.forName("utf-8"));
            os = new FileOutputStream(mainfestFile);
            while((zipEntry = zipInput.getNextEntry()) != null){
                if(zipEntry.getName().equals(fileName)){
                    is = zipFile.getInputStream(zipEntry);
                    int len;
                    while((len = is.read()) != -1){
                        os.write(len);
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(zipInput!=null){
                try {
                    zipInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
